﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.ComponentModel;

namespace CRM.Business_Object
{
    [DataObject]
    public class CustomerAccountObject
    {
        public string CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string BillingAddress{get;set;}
        public string ShippingAddress{get;set;}
        public string Email{get;set;}
        public string ContactNo{get;set;}
        public string Description{get;set;}
        public string Gender{get;set;}
        public string DateOfBirth { get; set; }

        public static void InsertCustomerList(CustomerAccountObject c)
        {     
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                SqlCommand comm= new SqlCommand();
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "Insert into CustomerAccount(CustomerID,CustomerName,BillingAddress,ShippingAddress,Email,ContactNo,Description,Gender,DateOfBirth) Values(@CustomerID,@CustomerName,@BillingAddress,@ShippingAddress,@Email,@ContactNo,@Description,@Gender,@DateOfBirth)";
                comm.Parameters.AddWithValue("@CustomerID",c.CustomerID);
                comm.Parameters.AddWithValue("@CustomerName", c.CustomerName);
                comm.Parameters.AddWithValue("@BillingAddress", c.BillingAddress);
                comm.Parameters.AddWithValue("@ShippingAddress", c.ShippingAddress);
                comm.Parameters.AddWithValue("@Email", c.Email);
                comm.Parameters.AddWithValue("@ContactNo", c.ContactNo);
                comm.Parameters.AddWithValue("@Description", c.Description);
                comm.Parameters.AddWithValue("@Gender", c.Gender);
                comm.Parameters.AddWithValue("@DateOfBirth", c.DateOfBirth);

                int rows = comm.ExecuteNonQuery();
                if(rows<0){

                }
            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }
            
        }

        public static DataTable viewCustomerList()
        {
            
            DataTable dt=null;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select * from CustomerAccount";
                SqlDataReader dr = comm.ExecuteReader();
                dt = new DataTable();
                dt.Load(dr);
            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }
        public static void updateCustomerList(CustomerAccountObject c)
        {
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Update CustomerAccount Set CustomerName=@CustomerName,BillingAddress=@BillingAddress,ShippingAddress=@ShippingAddress,Email=@Email,ContactNo=@ContactNo,Description=@Description,Gender=@Gender,DateOfBirth=@DateOfBirth where CustomerID=@CustomerID ";
                comm.Parameters.AddWithValue("@CustomerID",c.CustomerID);
                comm.Parameters.AddWithValue("@CustomerName",c.CustomerName);
                comm.Parameters.AddWithValue("@BillingAddress",c.BillingAddress);
                comm.Parameters.AddWithValue("@ShippingAddress",c.ShippingAddress);
                comm.Parameters.AddWithValue("@Email",c.Email);
                comm.Parameters.AddWithValue("@ContactNo",c.ContactNo);
                comm.Parameters.AddWithValue("@Description",c.Description);
                comm.Parameters.AddWithValue("@Gender",c.Gender);
                comm.Parameters.AddWithValue("@DateOfBirth",c.DateOfBirth);
                int rows = comm.ExecuteNonQuery();
            }
            catch
            {

            }
            finally
            {

            }
        }
        public static void DeleteCustomerList(CustomerAccountObject c)
        {

            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Delete from CustomerAccount where CustomerID=@CustomerID";
                comm.Parameters.AddWithValue("@CustomerID", c.CustomerID);
                int rows = comm.ExecuteNonQuery();

            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }
        }
        public static void getCustomerAccountById(string CustomerID)
        {
           /* CustomerAccountObject cust = null;
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select * from CustomerAccount where CustomerID=@CustomerID";
                comm.Parameters.AddWithValue("@CustomerID", CustomerID);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    cust = new CustomerAccountObject();
                    cust.CustomerID = (string)dr["CustomerID"];
                    cust.CustomerName = (string)dr["CustomerName"];
                    cust.BillingAddress = (string)dr["BillingAddress"];
                    cust.ShippingAddress = (string)dr["ShippingAddress"];
                    cust.Email = (string)dr["Email"];
                    cust.ContactNo = (string)dr["ContactNo"];
                    cust.Description = (string)dr["Description"];
                    cust.Gender = (string)dr["Gender"];
                    cust.DateOfBirth = (string)dr["DateOfBirth"];
                }

            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }
            return cust;*/
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CRMService"].ConnectionString);
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Delete from CustomerAccount where CustomerID=@CustomerID";
                comm.Parameters.AddWithValue("@CustomerID", CustomerID);
                int rows = comm.ExecuteNonQuery();

            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }
        }
       
       
    }
}